package exercicios.ex03.view;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * Servlet implementation class TabuabaEspecifica
 */
@WebServlet("/tabuadaEspecifica")
public class TabuabaEspecifica extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public TabuabaEspecifica() {
        super();
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		String valor = request.getParameter("valor");
		int intValor = 0;
		try {
			intValor = Integer.parseInt(valor);
		} catch (RuntimeException rtex) {
			intValor = -1;
		}
		
		PrintWriter out = response.getWriter();
		response.setContentType("text/html;charset=utf-8");
		out.println("<html>\n"+
				"<head>\n"+
				"<meta charset=\"utf-8\">\n"+
				"<title>Tabuada Específica</title>\n"+
				"</head>\n"+
				"<body>\n");
		
		if (valor==null || intValor<=0) {
			out.println("<h2>Valor não informado ou inválido, tente novamente</h2>\n"+
						"<a href=\"exercicio03\\tabuada.html\">Tente novamente</a>");
		} else {
			out.println("<table width=\"30%\" border=\"1\">");
			out.println("<th colspan=\"5\" align=\"center\">Tabuada de "+ intValor +"</th>");
			for (int i = 1; i <= 10; i++) {
				out.println("<tr align=\"center\"><td>"+intValor+"</td><td>X</td><td>"+i+"</td><td>=</td><td>"+(intValor*i)+"</td></tr>");
			}
			out.println("</table><p/>");
		}
		out.println("</body>\n"+
					"</html>\n");

	}

}
